package com.eagle.scali.contentcenter.sentinel;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * UrlCleaner 重新定义资源名字
 * 比如/shares/1 /shares/2 ，返回/shares/{number}
 */
@Component
@Slf4j
public class MyUrlCleaner implements UrlCleaner {
    @Override
    public String clean(String originUrl) {
        // 让 /shares/1 与 /shares/2 的返回值相同
        // 返回/shares/{number}

        String[] split = originUrl.split("/");

		return Arrays.stream(split).map(s -> {
			if (NumberUtils.isNumber(s)) {
				return "{number}";
			}
			return s;
		}).reduce((a, b) -> a + "/" + b)
				.orElse("");
	}
}
